package ServidorPackage;
import com.sun.corba.se.impl.orb.ParserTable;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;

public class Servidor {
    final private static int NUM_USERS=2;
    private static ArrayList clientes=new ArrayList<Socket>(); 
    public static void main(String[] args) throws IOException {
        ServerSocket servidor = new ServerSocket(5100);
        while (true) {
            Socket cliente = servidor.accept();
            clientes.add(cliente);
            System.out.println(cliente.getInetAddress().getHostAddress() + ": Conectado. Formando sala!");
            testaClientesPares();
        }
    
    }
    
    private static void testaClientesPares(){
        
        if (clientes.size() == NUM_USERS){
            
            for (int i = 0; i < NUM_USERS; i++){
                if (((Socket)clientes.get(i)).isClosed()){
                    clientes.remove(i);
                    return;
                }       
            }
            System.out.println("Sala Criada");
            Thread t = new Thread(new TratarCliente(clientes));
            t.start();
            clientes.clear();
            
        }
    }
    
}
